import json
import random

import requests
from DataTest.JsonData.read_json import load_json
from DataTest.ANR.path import *


def get_questions():
    """
    获取问题,随机通过选项生成答案：
    :return:
    """

    Questions_list = list()
    questions_list = list()
    Correct_list = list()

    self_token_list, match_token_list = load_json(json_data_path)

    get_question_url = 'http://124.220.33.63:8700/tacit/questions?num=5'

    for token in self_token_list:
        header = {
            'Authorization': f'Bearer {token}',
            'Content-Type': 'application/json',
        }

        question_res = requests.get(url=get_question_url, headers=header)

        question_data = question_res.json().get('data')

        options = [item.get('Options') for item in question_data]

        question = [item.get('Question') for item in question_data]

        answer_choice_list = [item.split('||') for item in options]

        answer_list = [random.choice(answer_list) for answer_list in answer_choice_list]

        questions_list.append(question)

        Correct_list.append(answer_list)

        for item in question_data:
            item['Correct'] = answer_list[question_data.index(item)]
            Questions_list.append(item)

    return Questions_list, questions_list, Correct_list


def tacit_q():
    """
    出题
    :return:
    """
    Questions_list, questions_list, Correct_list = get_questions()

    self_token_list, match_token_list = load_json(json_data_path)

    up_tacit_url = 'http://124.220.33.63:8700/tacit/questions'

    for token in self_token_list:
        n = 0
        header = {
            'Authorization': f'Bearer {token}',
            'Content-Type': 'application/json',
        }

        data = {
            "Questions": Questions_list
        }
        up_tacit_res = requests.post(url=up_tacit_url, headers=header, json=data)
        n += 1

        if up_tacit_res.status_code == 200:

            print(f'用户{token}出题成功，题目是{questions_list[n]},答案是{Correct_list[n]}')

        else:
            print(f'用户{token}出题失败')


def get_tacit_answer_id():
    """
    获取self_id出的题和题目id
    :return:
    """
    tacit_id_list = list()

    self_token_list, match_token_list = load_json(json_data_path)

    with open(json_data_path, 'r', encoding='utf-8') as file:
        data = json.load(file)
        self_id_list = [item.get('self_data').get('id') for item in data.get('data')]

    for i in range(len(match_token_list)):
        token = match_token_list[i]

        header = {
            'Authorization': f'Bearer {token}',
            'Content-Type': 'application/json',
        }

        user_id = self_id_list[i]

        get_id_url = f'http://124.220.33.63:8700/tacit/{user_id}/questions'

        id_res = requests.get(url=get_id_url, headers=header)
        try:
            id_list = id_res.json().get('data').get('id')

            tacit_id_list.append(id_list)
        except Exception as e:
            tacit_id_list.append([])
            print(e, '对方没有出题或获取题目失败')

    return tacit_id_list, self_id_list


def answer_tacit():
    """
    回答问题
    :return:
    """
    tacit_id_list, self_id_list = get_tacit_answer_id()

    Questions_list, questions_list, Correct_list = get_questions()

    self_token_list, match_token_list = load_json(json_data_path)

    for i in range(len(self_id_list)):
        token = match_token_list[i]
        other_id = self_id_list[i]

        tacit_id = tacit_id_list[i]
        correct = Correct_list[i]

        answer_data = dict(zip(tacit_id, correct))
        answer_tacit_url = f'http://124.220.33.63:8700/tacit/{other_id}/answer'

        data = {
            answer_data
        }
        answer_res = requests.post(url=answer_tacit_url, headers=token, json=data)

        if answer_res.status_code == 200:
            print(f'用户{token}回答成功，题目是{questions_list[i]},答案是{correct}')

        else:
            print(f'用户{token}回答失败')


if __name__ == '__main__':
    get_questions()
